package it.uniroma2.jcloudstests;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import it.uniroma2.cloud.PROVIDER;
import it.uniroma2.cloud.ProviderFactory;
import it.uniroma2.cloud.util.PropertiesMap;
import it.uniroma2.cloud.util.ProviderHelper;
import it.uniroma2.cloud.util.ProviderHelperFactory;
import it.uniroma2.cloud.util.PropertiesMap.CloudProviderProperty;

import org.jclouds.compute.ComputeService;
import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeMetadataBuilder;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.SshClient;

import com.google.common.base.Charsets;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;

import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Iterables.filter;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials;
import static org.jclouds.compute.predicates.NodePredicates.withIds;

;

public class TestNginx {

	/**
	 * @param args
	 * @throws RunScriptOnNodesException
	 */
	public static void main(String[] args) throws RunScriptOnNodesException {
		String lbId = "RegionOne/447e0446-b01b-4e50-b9a3-889f41116cee";
		PropertiesMap p = PropertiesMap.getInstance();
		ComputeService computeService = ProviderFactory
				.createComputeService(PROVIDER.OPENSTACK);
		ProviderHelper helper = ProviderHelperFactory
				.getProviderHelper(PROVIDER.OPENSTACK);
		NodeMetadata lbNodeMetadata = computeService.getNodeMetadata(lbId);

		SshClient client = null;
		try {
			client = computeService
					.getContext()
					.utils()
					.sshForNode()
					.apply(NodeMetadataBuilder
							.fromNodeMetadata(lbNodeMetadata)
							.credentials(
									LoginCredentials
											.builder()
											.user(p.get(CloudProviderProperty.OPENSTACK_IMAGE_USER))
											.privateKey(
													Files.toString(
															new File(
																	p.get(CloudProviderProperty.OPENSTACK_KEY_PATH)),
															Charsets.UTF_8))
											.authenticateSudo(true).build())
							.build());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			client.connect();
			client.put("/home/ubuntu/newNginx", Payloads
					.newStringPayload("upstream backend {\n"
							+ "  server 192.168.3.45:8080;\n"
							+ "  server 192.168.3.46:8080;\n" + "}\n"
							+ "server {\n" + "  location / {\n"
							+ "    proxy_pass  http://backend;\n" + "  }\n"
							+ "}\n"));

			helper.runScriptOnInstance(
					computeService,
					lbId,
					Statements
							.exec("cp /home/ubuntu/newNginx /etc/nginx/sites-enabled/awslb"));
			helper.runScriptOnInstance(computeService, lbId,
					Statements.exec("kill -HUP `cat /var/run/nginx.pid`")); // OPPURE
			// "service nginx reload"
		} finally {
			if (client != null)
				client.disconnect();
			computeService.getContext().close();
		}

	}
}
